/*
 * MyButtonSpecialInfo.java
 *
 * Created on May 17, 2000, 12:42 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author ayman
 * In this class we save the special properties of the button "MyButton" which 
 * doesn't exit in the JComponent.
 */
public class MyLabelSpecialInfo {
    
    // The info about the button
    private MyLabelContainer mylabel;
    private JLabel jlabel;
    private Vector infoVector = new Vector ();
    /************************************************
     *              Vector elements
     * 1- label for text 
     * 2- textField for text
     * 3-label for vertical aligment
     * 4- combobox
     * 5- label for horizontal aligment 
     * 6- combobox
     * 7- label for the icon name 
     * *- panel
     *   8 text for the icon path 
     *   9 button for the file chooser
     *
     ************************************************/
    private JFrame propertiesFrame;
    
    /** Creates a new instance of MyButtonSpecialInfo */
    public MyLabelSpecialInfo(MyLabelContainer label,JFrame propertiesFrame) {
        //System.out.println ("to get button info");
        this.propertiesFrame = propertiesFrame;
        this.mylabel = label ;       
        jlabel = (JLabel)label.getComponent();
        HandleCompText();
        HandleVerticalTextPosition();
        HandleHorizontalTextPosition();
        HandleIconOnButton();
        //HandleIsEnable();
        HandleFont();
        //getInfoVector();
    }
    

    
    /**
     * This Function is used to retrieve the info specific for the JButton
     * By applying some methods used to get those info
     */
    public Vector getInfoVector () {
        return infoVector;
    }
    
    /**
     * This function is used to read the new inforamtion and set it again
     */
    public void getAndSave () {
        String textOnButton;
        int typeOfVTxtPos;
        int typeOfHTxtPos;
        String iconFileName;
        int isenable;
        
        
        // get the info of the input
        textOnButton =  ((JTextField)infoVector.elementAt(1)).getText();
        typeOfVTxtPos = ((JComboBox)infoVector.elementAt(3)).getSelectedIndex();
        typeOfHTxtPos = ((JComboBox)infoVector.elementAt(5)).getSelectedIndex();
        iconFileName = ((JTextField)((JPanel)infoVector.elementAt(7)).getComponent(0)).getText();
        //isenable = ((JComboBox)infoVector.elementAt(9)).getSelectedIndex();
        
        
        // setting the info
        jlabel.setText(textOnButton);
        switch (typeOfVTxtPos) {
            case 0 : {
                jlabel.setVerticalTextPosition(SwingConstants.CENTER);
                break;
            }
            case 1 : {
                jlabel.setVerticalTextPosition(SwingConstants.TOP);
                break;
            }
            case 2 : {
                jlabel.setVerticalTextPosition(SwingConstants.BOTTOM);
                break;
            }
        }
        switch (typeOfHTxtPos) {
            case 0 : {
                jlabel.setHorizontalTextPosition(SwingConstants.RIGHT);
                break;
            }
            case 1 : {
                jlabel.setHorizontalTextPosition(SwingConstants.LEFT);
                break;
            }
            case 2 : {
                jlabel.setHorizontalTextPosition(SwingConstants.CENTER);
                break;
            }
            case 3 : {
                jlabel.setHorizontalTextPosition(SwingConstants.LEADING);
                break;
            }
            case 4 : {
                jlabel.setHorizontalTextPosition(SwingConstants.TRAILING);
                break;
            }
        }
        
        
        // setting the icon name
        Toolkit kit = Toolkit.getDefaultToolkit ();
       // if (!iconFileName.equals("IMAGE")) {
            //System.out.println("  tamer ****************");
        mylabel.setPhotoPath(iconFileName);
            Image img1 = kit.getImage(iconFileName);
            ImageIcon icon1 =new  ImageIcon (img1);
            jlabel.setIcon(icon1);
       // }
        
        
        //Setting the (enable / disable ) button
        /*if (isenable==0) {
            jlabel.setEnabled(true);
        } 
        else {
            jlabel.setEnabled(false);
        }*/
        
        
        //for (int i=3 ; i < infoVector.size() ; i++,i++) {
        //    System.out.println ("1- the select num is " + ((JComboBox)infoVector.elementAt(i)).getSelectedIndex());
       // }
    }
    
    //**************************************************************************
    /**
     *This function get text on the component
     */
    private void HandleCompText () {
        JLabel compText = new JLabel ("Component text :");
        infoVector.addElement(compText);
        JTextField compXTxtField = new JTextField (jlabel.getText());
        infoVector.addElement(compXTxtField);
   }
    
    
    //**************************************************************************
    /**
     * This function get the text vertical aligment
     */
    private void HandleVerticalTextPosition () {
        //JButton buttonComp = (JButton) button.getComponent();
        JLabel compText = new JLabel ("Veritcal text pos :");
        infoVector.addElement(compText);
        String Aligment [] = new String [3];
        Aligment [0] = "CENTER";
        Aligment [1] = "TOP";
        Aligment [2] = "BUTTOM";
        JComboBox compAligmentChoises = new JComboBox (Aligment);
        //System.out.println (" the aligment is  " + jlabel.getVerticalTextPosition());
        compAligmentChoises.setSelectedIndex(jlabel.getVerticalTextPosition());
        infoVector.addElement(compAligmentChoises);
        
    }
    
    //**************************************************************************
    /**
     * This function get the text horizontal aligment
     */
    private void HandleHorizontalTextPosition () {
        //JButton buttonComp = (JButton) button.getComponent();
        JLabel compText = new JLabel ("Horizontal text pos :");
        infoVector.addElement(compText);
        String Aligment [] = new String [5];
        Aligment [0] = "RIGHT";
        Aligment [1] = "LEFT";
        Aligment [2] = "CENTER";
        Aligment [3] = "LEADING";
        Aligment [4] = "TRAILING";
        JComboBox compAligmentChoises = new JComboBox (Aligment);
        //System.out.println (" the aligment is  " + jlabel.getHorizontalTextPosition());
        // transfer the constance into number to be able to load them in combobox
        int selectedIndex = 0;
        switch (jlabel.getHorizontalTextPosition()) {
            case 0 : {
                selectedIndex = 2 ; break;
            }
            case 2 : {
                selectedIndex = 1 ; break;
            }
            case 4 : {
                selectedIndex = 0 ; break;
            }
            case 10 : {
                selectedIndex = 3 ; break;
            }
            case 11 : {
                selectedIndex = 4 ; break;
            }
            
        }
        compAligmentChoises.setSelectedIndex(selectedIndex);
        infoVector.addElement(compAligmentChoises);
        
    }
    
     //**************************************************************************
    /**
     * This function get the icon on the button
     */
    private void HandleIconOnButton () {
        JLabel iconName = new JLabel ("Icon Name :");
        infoVector.addElement(iconName);
        JPanel iconPanel = new JPanel ();
        JTextField iconNameText = new JTextField (mylabel.getPhotoPath());
        JButton fileChooser = new JButton ("...");
        //FileChooserButtonHandler handle = new FileChooserButtonHandler ();
        fileChooser.addMouseListener(new FileChooserButtonHandler(iconNameText));
        fileChooser.setSize(100,100);
        iconPanel.add(iconNameText);
        iconPanel.add(fileChooser);
        infoVector.addElement(iconPanel);
        
        //infoVector.addElement(fileChooser);
        
           
            
    }
     //**************************************************************************
    /**
     * The class implements a click listener to perfomr the file choose window
     * actiom.
     *
     */
    class FileChooserButtonHandler extends MouseAdapter {
        private JTextField iconNameText;
        public FileChooserButtonHandler (JTextField iconNameText) {
            this.iconNameText = iconNameText;
        }
                public void mouseClicked(MouseEvent e) {
                    String fileName;
                    JFileChooser file = new JFileChooser ();
                    file.showSaveDialog(propertiesFrame);
                    fileName = file.getSelectedFile().getPath();
                    iconNameText.setText(fileName);                    
                    //System.out.println (" the file is "+ fileName);
                }
            }
    

    
    //**************************************************************************
    /**
     * This function get is enable the button 
     */
    private void HandleFont () {
        JLabel labelFont = new JLabel ("Font Name  :");
        infoVector.add(labelFont);
        JButton openFontChooser = new JButton (" FONT ");
        openFontChooser.addMouseListener(new HandlePressForFont());
        infoVector.add(openFontChooser);
    }
    /**
     * Class for the font selection
     */
    
    class HandlePressForFont extends MouseAdapter {
        
        public void mouseClicked(MouseEvent e) {
            AdvancedFontChooser.getAndSetFont(jlabel) ;
        }
    }
    
    
   
    
}
